package com.beagle.njddapp.ui.activity.oil;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.oil.OilBean;
import com.beagle.njddapp.ui.presenter.OilManagerPresenter;
import com.beagle.njddapp.ui.view.OilManagerView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-18 21:41
 * 加油车管理
 */
public class OilDeviceManagerActivity  extends BaseMvpActivity<OilManagerView, OilManagerPresenter> implements OilManagerView {

    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.iv_oil)
    ImageView ivOil;
    @BindView(R.id.rl_oil)
    RelativeLayout rlOil;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_bianhao)
    TextView tvBianHao;
    private OilBean oilbean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_oilmanager;
    }

    @Override
    public void init() {
        StatusBarUtil.setPaddingSmart(this,container);
        container.setBackgroundColor(Color.WHITE);
        StatusBarUtil.darkMode(this);
        tvTitle.setText("加油车管理");
        presenter.myCar();
    }

    @Override
    public OilManagerView getMvpView() {
        return this;
    }

    @Override
    public OilManagerPresenter createPresenter() {
        return new OilManagerPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }



    @OnClick({R.id.common_back_iv,R.id.common_tv_right,R.id.rl_oil})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.rl_oil :
                startActivity(new Intent(this,OilUpdateActivity.class).putExtra("data",oilbean));
                break;
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.common_tv_right:
                if (rlOil.getVisibility()==View.VISIBLE){
                    startActivityForResult(new Intent(this,OilAddActivity.class),302);
                }else {
                    startActivityForResult(new Intent(this,OilAddActivity.class),302);

                }
                break;
        }
    }

    @Override
    public void showOild(OilBean result) {
        oilbean=result;
        if (result==null){
            rlOil.setVisibility(View.GONE);
            tvRight.setText("添加");
        }else {
            rlOil.setVisibility(View.VISIBLE);
            tvRight.setText("解绑");
            tvNumber.setText(result.getCarNum());
            tvBianHao.setText(result.getEngineNum());
            tvCount.setText(result.getOilMeter()+"");
            tvTime.setText(result.getProductionDate());
            Glide.with(this).load(result.getPictureUrl()).into(ivOil);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==303){
            if (presenter!=null){
                presenter.myCar();
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        presenter.myCar();
    }
}
